<?php
$config = include 'config.php';

// get facebook status
function parse_signed_request($signed_request, $secret) {
  @list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    error_log('Unknown algorithm. Expected HMAC-SHA256');
    return null;
  }
  // check sig
  $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  if ($sig !== $expected_sig) {
    error_log('Bad Signed JSON signature!');
    return null;
  }

  return $data;
}
function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}

if(isset($_REQUEST['signed_request'])) {
    $config['facebookstatus'] =  parse_signed_request($_REQUEST['signed_request'], $config['facebook_app_secret']);      
    if($config['facebookstatus']['page']['admin'] == "1") {
        $config['facebookstatus']['page']['liked'] = 1;
    }
} else {
    $config['facebookstatus'] = false;
}

error_reporting(E_ALL ^ E_NOTICE);
//error_reporting(E_ALL);
//ini_set('display_errors','On');

require_once '../../libs/system_session.php';
require_once '../../libs/system_view.php';
require_once '../../libs/system_db.php';
require_once '../../libs/system_controller.php';

$dbConfig = include '../../config.php';

$config = array_merge($config, $dbConfig);
//echo "<pre>";print_r($config);die;
require_once 'controller.php';
$controller = new referer_action_controller($config);
$controller->handle();
?>
